/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ueb14.uebTree;

import de.htw.saarland.stl.Stdin;
import ueb14.htw.io.StringLineReader;

/**
 *
 * @author steffenschuhmann
 */
public class Dialog {
    
    protected String dLine = null;
    protected TreeManagment manager = null;
    
    public void setUp() {
        dividingLine();
        run();
    }
    
    public void run() {
        String file = "";
        String command = null;
        String name = null;
        int counter = 0;
        boolean end = false;
        boolean eof = false;
        StringLineReader reader = null;
        int operation = -1;

        while (!end) {
            System.out.println();
            System.out.println(TreeProperties.MESSAGE);
            file = readCommand();
            try {
                reader = new StringLineReader(file);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if (file.equalsIgnoreCase(TreeProperties.END)) {
                end = true;
            } else {
                try {
                    while (!eof) {
                        command = readLine(counter, reader);
                        if (command == null) {
                            eof = true;
                        } else {
                            operation = process(command);
                            name = trimName(command);
                            execute(operation, name);
                            counter++;
                        }
                    }
                    System.out.println(manager.toString());
                } catch (Exception e) {
                    System.out.print(e.getMessage());
                }
            }

        }
        System.out.println("Das Programm wurde beendet!");
    }
    
    /**
     * Dummi-Method should be overwritten for the specific use.
     * @param command The command which will be processed in this method. 
     */
    public void execute(int operation, String command) {
        
    }

    public int process(String command) {
        command.trim();
        if(command.charAt(0) == TreeProperties.ADD)
            return 1;
        if(command.charAt(0) == TreeProperties.REMOVE)
            return 0;
        
        return -1;
    }
    
    /**
     * Reads a line and returns it.
     */
    public String readCommand() {
        String command = null;
        command = Stdin.readString();
        return command;
    }
    
    public void dividingLine() {
        StringBuilder divLine = new StringBuilder();
        for(int i = 0; i <= TreeProperties.LAENGE; i++) {
            divLine.append(TreeProperties.DCHAR);
        }
        dLine = divLine.toString();
    } 
    
    protected String trimName(String command) {
        if (command == null) {
            System.err.println("String is empty" );
        } else if (command.equals(" ")) {
            System.out.println("String is empty!");
        } else {
            command = command.split("[+-]")[1];
            return command;
        }
        return null;
    }
    
    protected String readLine(int lineNumber, StringLineReader reader) {
        String line = null;
        
        try {
            line = reader.read(lineNumber);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        
        return line;
    }
    
    protected void remove(String name) {
        if(manager == null) {
            System.out.println("No items in structure!");
        } else {
            try {
            manager.del(name);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println(manager.toString());
        }
    }
    
    protected void add(String name) {
        try {
            if (manager == null) {
                try {
                    manager = new TreeManagment(name);
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                }
                System.out.println(manager.toString());
            } else {
                try {
                manager.add(name);
                System.out.println(name +" added!");
                System.out.println(manager.toString());
                } catch(Exception e) {
                    e.printStackTrace();
                    System.out.println(e.getMessage());
                }
            }
        } catch(Exception pe) {
            System.out.println(pe.getMessage());
        } 
    }
    
}
